<?php
/*
 * Created on Dec 14, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
if (isset ($_GET['export'])){
	$con = mysql_connect("localhost", "root", "bytewalla5");
	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("ssa", $con);

	// select last 5 entries
	//SELECT distinct timestamp,light FROM data WHERE spot_id="30" AND timestamp between "2011-11-30 08:15:41" and "2011-11-30 08:16:05" order by timestamp
	$query = $_GET['export'];
	//echo $query;
	$res = mysql_query($query);
	// put all the data inside an array
	$arr = array ();
	while ($obj = mysql_fetch_object($res)) {
		$arr[] = $obj;
	}

	for ($i = 1; $i <= sizeof($arr); $i++) {
// add your modification here
		$data .="\n" . $arr[$i -1]->caseid . "\t" . $arr[$i -1]->firstname . "\t\t" . $arr[$i -1]->lastname ."\t\t". $arr[$i -1]->age."\t" .$arr[$i -1]->gender ."\t".$arr[$i -1]->weight ."\t".
                $arr[$i -1]->prevdisease ."\t".$arr[$i -1]->bp ."\t". $arr[$i -1]->cough ."\t". $arr[$i -1]->addiction ."\t\t".$arr[$i -1]->allergy ."\t\t".
                $arr[$i -1]->pain ."\t\t".$arr[$i -1]->comments ."\t\t".$arr[$i -1]->date .  "\n\n";

	}

       // $prevdisease','$sweating','$bp','$cough','$addiction','$allergy','$pain','$comments','$date')"

	mysql_close($con);
	//print $data;
	
	$filename =  time(). ".txt";
	$fileposition = "/var/www/SSA/download/" . $filename;

	$f = fopen($fileposition, 'w');
	fwrite($f, "SSA Report\n");
// add here one sentence
	fwrite($f,"\n" . "Case Id\t" ."FirstName\t" ."LastName\t" ."Age\t" . "Gender\t" ."Wt.\t" ."Disease\t" ."BP\t" . "Cough\t". "Addictions\t" ."Allergies\t" . "Pain\t" . "Feedback\t" . "Date\t". "\t\n");
	fwrite($f, $data);
	fclose($f);
	
}
if (isset ($_GET["desteid"])) {
		include_once ("httpsqs/httpsqs_client.php");
		$httpsqs_host = "localhost";
		$httpsqs_port = "1218";
		$httpsqs_auth = "";
		$httpsqs_charset = "utf-8";
		$httpsqs = new httpsqs($httpsqs_host, $httpsqs_port, $httpsqs_auth, $httpsqs_charset);
		//curl -d "dtncp /home/hao/test.txt dtn://hao.bytewalla.com"  "http://localhost:1218/?charset=utf-8&name=&opt=put"
		$httpsqs->reset("text_java_request");
		$cmd = "dtncp ".$_GET["fileposition"]." ". $_GET["desteid"];
		
		$result = $httpsqs->put("text_java_request", $cmd);
		header("Location:dtncpres.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Query info</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript" src="js/jquery-1.6.js" ></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
<script type="text/javascript" src="js/Swis721_Cn_BT_400.font.js"></script>
<script type="text/javascript" src="js/Swis721_Cn_BT_700.font.js"></script>
<script type="text/javascript" src="js/tabs.js"></script>

  <!--[if lt IE 9]>
  	<script type="text/javascript" src="js/html5.js"></script>
	<style type="text/css">
		.bg{ behavior: url(js/PIE.htc); }
	</style>
  <![endif]-->
	<!--[if lt IE 7]>
		<div style=' clear: both; text-align:center; position: relative;'>
			<a href="http://www.microsoft.com/windows/internet-explorer/default.aspx?ocid=ie6_countdown_bannercode"><img src="http://www.theie6countdown.com/images/upgrade.jpg" border="0"  alt="" /></a>
		</div>
	<![endif]-->

</head>

<body id="page2">
	<div class="body1">
	<div class="body2">
	<div class="body5">
		<div class="main">
<!-- header -->
			<header>
				<div class="wrapper">
				<h1><a href="index.html" id="logo">Progress Business Company</a></h1>
				<nav>
					<ul id="menu">
						<li id="nav1"><a href="index.html">Home<span>Welcome!</span></a></li>
						<li id="nav2" class="active"><a href="ssa.html">SSA<span>Fresh</span></a></li>
						<li id="nav3"><a href="nmt.html">NMT<span>for you</span></a></li>
						<li id="nav4"><a href="wsn.html">WSN<span>The best</span></a></li>
						<li id="nav5"><a href="Contacts.html">Contacts<span>Our Address</span></a></li>
					</ul>
				</nav>
				</div>
			</header><div class="ic"></div>
<!-- header end-->
		</div>
	</div>
	</div>
	</div>
	<div class="body3">
		<div class="main">
<!-- content -->
			<article id="content">
				<div class="wrapper tabs">
     				<section class="col1">
     				<h2 class="under">Download Data</h2>
     				
     				<a href="download/<?=$filename?>">you can download data here</a>
     				<h2 class="under">Distribute Data</h2>
     				You can distribute data over DTN
					<form id="ContactForm" action="exportData.php" method="get">
					 	Destination EID <input  class="input" type="input" name="desteid" />(please give the destination endpoint id)
					 	<input type="hidden" name="fileposition" value=<?=$fileposition?> />
					 	<a href="#" onClick="document.getElementById('ContactForm').submit()">Distribute</a>
					 	
					</form>
							
					</section>


				</div>

			</article>
		</div>
	</div>
	<div class="body4">
		<div class="main">
			<article id="content2">
				<div class="wrapper">
					<section class="col3 pad_left2">
						<h4>Address</h4>
						<ul class="address">
							<li><span>Country:</span>Sweden</li>
							<li><span>City:</span>Stockholm</li>
							<li><span>Phone:</span></li>
							<li><span>Email:</span><a href="mailto:">mail@bytewalla.com</a></li>
						</ul>
					</section>
					<section class="col3 pad_left2">
						<h4>Follow Us</h4>
						<ul id="icons">
							<li><a href="#"><img src="images/icon1.jpg" alt="">Facebook</a></li>
							<li><a href="#"><img src="images/icon2.jpg" alt="">Twitter</a></li>

						</ul>
					</section>

				</div>
			</article>
<!-- content end -->
		</div>
	</div>
		<div class="main">
<!-- footer -->
   
<!-- footer end -->
		</div>
<script type="text/javascript"> Cufon.now(); </script>
<script>
	$(document).ready(function() {
		tabs.init();
	})
</script>
</body>
</html>